package com.codecash.mapper;

import com.codecash.entity.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {
    // 新增帖子
    @Insert("INSERT INTO posts(content, post_type, author_id, like_count, view_count, video_url, create_time, update_time, title) " +
            "VALUES(#{content}, #{postType}, #{authorId}, #{likeCount}, #{viewCount}, #{videoUrl}, #{createTime}, #{updateTime}, #{title})")
    void add(Post post);


    // 更新帖子
    @Update("UPDATE posts SET title = #{title}, content = #{content}, cover_img = #{coverImg}, " +
            "like_count = #{likeCount}, view_count = #{viewCount}, video_url = #{videoUrl}, " +
            "post_type = #{postType}, update_time = #{updateTime} WHERE id = #{id}")
    void update(Post post);


    //  删除帖子
    @Delete("DELETE FROM posts WHERE id = #{id}")
    void delete(Long id);


    //查询帖子
    @Select("SELECT id, title, content, post_type, author_id, like_count, view_count, video_url, create_time, update_time " +
            "FROM posts WHERE id = #{id}")
    Post getPostById(Long id);

    // 查询所有帖子
    @Select("SELECT id, title, content, post_type, author_id, like_count, view_count, video_url, cover_img, create_time, update_time " +
            "FROM posts ORDER BY create_time DESC")
    List<Post> findAllPosts();

    //查询用户帖子
    @Select("SELECT id, title, content, post_type, author_id, like_count, view_count, video_url, cover_img, create_time, update_time " +
            "FROM posts WHERE author_id = #{currentUserId} ORDER BY create_time DESC")
    List<Post> findAllmyPosts(Long currentUserId);
}

